<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view class="address-btn u-flex u-row-center" v-if="!address">
        <u-button
          shape="circle"
          size="medium"
          :custom-style="{ backgroundColor: '#fff!important' }"
          type="error"
          plain
          @click="handleChooseAddress"
          >获取收获地址</u-button
        >
      </view>
      <view v-else class="address-detail">
        <view class="address-details">{{ addressDetail }}</view>
        <view class="address-user"
          >{{ address.userName }} {{ address.telNumber }}</view
        >
      </view>
    </view>
    <u-line color="#eee"></u-line>
    <!-- 2 已选商品 -->
    <view class="goods-list">
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view class="cart-item" v-for="item in checkedCartList" :key="item.goods_id">
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              width="191rpx"
              height="191rpx"
              :src="item.goods_small_logo"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">x{{ item.nums }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 3 统计 -->
    <view class="statistics">
      <view class="all-price">
        合计: <text>￥{{checkedPrice}}</text>
      </view>        
      <view class="all-count">
        <u-button
          type="error"
          shape="circle"
          @click="handlePay"
        >去结算({{checkedCount}})</u-button>
      </view>
    </view>
  </view>
</template>

<script>
import {mapGetters,mapState,mapActions} from "vuex"
export default {
  data() {
    return {
      address: null,
    };
  },
  computed: {
    // 详情地址
    addressDetail() {
      if (!this.address) return "";
      return (
        this.address.provinceName +
        this.address.cityName +
        this.address.countyName +
        this.address.detailInfo
      );
    },
    ...mapState("user",["token"]),
    // 选中的商品
    ...mapGetters("cart",["checkedCartList", "checkedPrice", "checkedCount"])

  },
  methods: {
      ...mapActions("user",["fetchToken"]),
    //
    async handleChooseAddress() {
      const [err, res] = await uni.chooseAddress();
      // console.log(res);
      if (!err) {
        this.address = res;
      }
    },
    // 点击结算按钮
    async handlePay(){
        // 0 判断有没有收货地址
        if(!this.address){
            uni.showToast({
                title:"你还没有收货地址",
                icon:"none"
            })
            return
        }
        // 1.5 判断当前有没有token 有 就直接使用 没有 获取即可
        if(!this.token){
            // 获取token
            await this.fetchToken()
            // this.token=真正的token
        }
        // // 1 获取用户信息
        // const [err,{ encryptedData, rawData, iv, signature }]=await uni.getUserProfile({
        //   desc: "用于提供更好的服务",
        // });
        // // console.log(encryptedData, rawData, iv, signature);

        // // 2 获取code临时凭据
        // const [err2,{code}]=await uni.login()
        // // console.log(code);

        // // 3 获取token  后端没有写好代码！！ 正常是每一次获取都是ok！！！
        // // 以点击两次 为准
        // const {token}=await this.$u.post("/users/wxlogin",{encryptedData, rawData, iv, signature,code})
        // console.log(token);

        // 4 准备创建订单要的参数
        const orderParams={
            order_price:this.checkedPrice,
            // A|B|C
            consignee_addr:`${this.addressDetail}|${this.address.userName}|${this.address.telNumber}`,
            goods:this.checkedCartList.map((item)=>({ goods_id: item.goods_id,
          goods_number: item.nums,
          goods_price: item.goods_price,})),
        };

        // 5 发送请求 获取订单编号axios传参的写法 我不确定！！
        const {order_number}=await this.$u.post("/my/orders/create",orderParams);
        // console.log(res);

        // 6 创建支付流程 获取支付参数
        const {pay}=await this.$u.post("/my/orders/req_unifiedorder",{order_number})
        // console.log(res);

        // 7 调起微信支付
        const res7=await uni.requestPayment(pay)
        // console.log(res7);
    }
  },
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;
      .goods-chk {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-img {
        flex: 2;

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-info {
        color: #666;
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .goods-name {
        }

        .goods-price-num {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: $themeColor;
          }

          .goods-num-tool {
            width: 140rpx;
            height: 40rpx;

            display: flex;
            justify-content: flex-end;

            .goods-vnum {
            }
          }
        }
      }
    }
 // 结算
  .statistics {
    display: flex;
    background-color: #fff;
    position: fixed;
    width: 100%;
    height: 111rpx;
    bottom: 0;
    left: 0;
    border-top: 1rpx solid #ccc;
    align-items: center;
    padding: 0 15rpx;
    font-size: 21rpx;
  .all-chk {
    
  }

  .all-price {
    flex: 1;
    color: #666;
    font-size: 26rpx;
    text {
      color: $themeColor;
      font-size: 30rpx;
    }
  }

  .all-count {
    
  }
  }
</style>